package com.silong.swing.myWindows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by 龙哥 on 2022-01-17 15:38
 * Silong Software.
 */

public class RadioFrame extends JFrame {

    JButton button1;
    JButton button2;
    JRadioButton rbtMale;
    JRadioButton rbtFeMale;
    JLabel lblSex;
    ButtonGroup bgSex;

    public RadioFrame(){
        init();
    }

    private void init(){

        button1 = new JButton("确定");
        button2 = new JButton("退出");
        rbtFeMale = new JRadioButton("女",false);
        rbtMale = new JRadioButton("男", true);
        lblSex = new JLabel("请选择性别");

        //按钮组要将单选按钮加入，形成互斥，加入之前必须要实例化 JRadioButton
        bgSex = new ButtonGroup();
        bgSex.add(rbtFeMale);
        bgSex.add(rbtMale);

        Container window = this.getContentPane();
        this.setSize(800, 600);
        this.setLocationRelativeTo(null); //距中
        this.setTitle("自定义控件大小位置");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出窗口关闭程序
        //设置布局为空，优点:摆放灵活，缺点：代码量大，自适应能力差。
        this.setLayout(null);

        lblSex.setLocation(100,50); //设置按钮位置
        lblSex.setSize(200,50); //设置按钮大小
        lblSex.setFont(new Font("黑体", Font.BOLD, 20));

        //单选钮
        rbtMale.setLocation(120,100); //设置按钮位置
        rbtMale.setSize(80,50); //设置按钮大小
        rbtMale.setFont(new Font("黑体", Font.BOLD, 20));
        rbtMale.setActionCommand("男"); //可以使用 getSelection.getActionCommand
        rbtFeMale.setLocation(200,100); //设置按钮位置
        rbtFeMale.setSize(80,50); //设置按钮大小
        rbtFeMale.setFont(new Font("黑体", Font.BOLD, 20));
        rbtFeMale.setActionCommand("女"); //可以使用 getSelection.getActionCommand

        //自定义按钮的位置 ，大小
        button1.setLocation(100,180); //设置按钮位置
        button1.setSize(200,50); //设置按钮大小
        button1.setFont(new Font("黑体", Font.BOLD, 20));
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //打印出用户选择
                if(rbtMale.isSelected()){
                    System.out.println("您选择的是：" + rbtMale.getText());
                }else if(rbtFeMale.isSelected()){
                    System.out.println("您选择的是：" + rbtFeMale.getText());
                }else{
                    System.out.println("您没有选择！");
                }
            }
        });
        button2.setFont(new Font("黑体", Font.BOLD, 20));
        button2.setBounds(400, 180, 200, 50); //一次性设置位置与大小
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        window.add(button1);
        window.add(button2);
        window.add(lblSex);
        window.add(rbtFeMale);
        window.add(rbtMale);
    }

    public static void main(String[] args) {
        RadioFrame myFrame = new RadioFrame();
        myFrame.setVisible(true);
    }
}
